import { IMenuRoute } from "@/types";
import { ReactNode } from "react";
import { NavigateFunction } from "react-router-dom";

interface IMenuItem {
    role?: string;
    key: string;
    type?: string;
    label: any;
    children?: IMenuItem[];
    icon?: ReactNode;
    onClick?: () => void;
}

export const breadcrumbMap = new Map<string, any[]>([]);

/**
 * 转换路由配置为menu配置
 */
export function tsRoutes2Menus(routes: IMenuRoute[], navigate: NavigateFunction, routePrefix = '', parentRoute?: IMenuRoute) {
    const menuConfig: IMenuItem[] = routes.map(
        route => {
            if (parentRoute) {
                breadcrumbMap.set(routePrefix + route.route!, [parentRoute.icon, route.label]);
            }

            return {
                key: route.key,
                type: route.type,
                label: route.label,
                icon: route.icon,
                role: route.role,
                children: route.children ? tsRoutes2Menus(route.children!, navigate, routePrefix + (route.route || ''), route) : undefined,
                onClick: route.route && !route.disableRoute ? () => {
                    navigate(routePrefix + route.route!);
                } : undefined,
            };
        }
    );
    return menuConfig;
}

/**
 * 是否为稳定版本路由
 */
export function isVpStableRoute() {
    const { pathname } = window.location;
    return pathname.includes('/vp/stable');
}